import {getTheme,saveTheme,commonUpload} from '../services/API';
import { message } from 'antd';

export default {
    namespace:"theme",
    state:{
        themeList:[],
        selectTheme:0
    },

    effects:{
        *fetch({payload},{put,call}){
            const res = yield call(getTheme,payload);
            if(res.code === 200){

                yield put({
                    type:'save',
                    payload:{
                        themeList:res.data
                    }
                })
            }

        },
        *saveTheme({payload},{put,call}){
            const res = yield call(saveTheme,payload);
            if(res.code === 200){
                message.success(res.msg || 'Operation succeeded'); 
                yield put({
                    type:'fetch',
                });
            }
        },
        *commonUpload({payload,callback},{put,call}){
            const res = yield call(commonUpload,payload);
            if(res.code === 200){
                message.success(res.msg || 'Operation succeeded'); 
                callback&&callback(res)
            }else{
                message.error(res.msg || '失败'); 
            }
            
        }
    },

    reducers:{
        save(state, action) {
            return { ...state, ...action.payload };
        },
    },
    subscriptions: {
        setup({ dispatch, history }) {
            history.listen(({ pathname }) => {
                if (pathname === '/theme') {
                    dispatch({
                        type: "theme/fetch",
                    });
                }
            });
        },
    },
}